package gui.tools;

import graphics.SceneGraph;
import graphics.tools.OrbitBehavior;
import gui.Frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.j3d.Canvas3D;

/**
 * For handling actions made by the programs menus and toolbars.
 * 
 * @author: Patrik Bakke
 * @version: %I%, %G%
 */
public class ActionHandler implements ActionListener {

	private Frame main;
	private Canvas3D cv;
	private OrbitBehavior ob;
	private SceneGraph sg;
	private double xCurrent;
	private double yCurrent;

	/**
	 * Constructor, setting the needed information.
	 * 
	 * @param main
	 *            An instant of the Frame for the GUI
	 */
	public ActionHandler(Frame main) {
		this.main = main;
	}

	public void getAdditional() {
		ob = main.getUniverse().getSceneGraph().getOrbit();
		sg = main.getUniverse().getSceneGraph();
		cv = main.getUniverse().getSceneGraph().getCanvas();
	}

	/**
	 * For performing actions made by the program.
	 * 
	 * @param e
	 *            The event to be processed
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("Exit")) {
			main.dispose();
			System.exit(0);
		}

		if (e.getActionCommand().equals("Zoom In")) {
			ob.doZoomOperations(-50);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}

		if (e.getActionCommand().equals("Zoom Out")) {
			ob.doZoomOperations(50);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}

		if (e.getActionCommand().equals("Rotate CW")) {
			ob.setLongditude(-50);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}

		if (e.getActionCommand().equals("Rotate CCW")) {
			ob.setLongditude(50);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}

		if (e.getActionCommand().equals("Rotate Up")) {
			ob.setLatitude(-50);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}

		if (e.getActionCommand().equals("Rotate Down")) {
			ob.setLatitude(50);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}

		if (e.getActionCommand().equals("Reset")) {
			ob.goHome();
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}
		if (e.getActionCommand().equals("Up")) {
			yCurrent = ob.getYtrans();
			ob.setYtrans(yCurrent -= .1);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}
		if (e.getActionCommand().equals("Down")) {
			yCurrent = ob.getYtrans();
			ob.setYtrans(yCurrent += .1);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}
		if (e.getActionCommand().equals("Left")) {
			xCurrent = ob.getXtrans();
			ob.setXtrans(xCurrent -= .1);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}
		if (e.getActionCommand().equals("Right")) {
			xCurrent = ob.getXtrans();
			ob.setXtrans(xCurrent += .1);
			sg.setOrbit(ob);
			ob.integrateTransforms();
		}
	}
}
